package com.hlee.xml;
import java.io.File;
import java.io.Reader;
import java.io.StringReader;

import org.apache.commons.digester.Digester;


public class XmlDigesterExample
{
    public static void main(String ar[]) throws Exception
    {
        parseExample();

        parseIncomingImage();
        
    }

    private static void parseIncomingImage() throws Exception
    {
        Digester digester = new Digester();
        digester.setValidating(false);
        
        // Define a set of rules that Digester will use to parse the incomingImage XML
        
        digester.addObjectCreate("incomingImage", IncomingImage.class);
        digester.addBeanPropertySetter("incomingImage/imageType", "imageType");
        digester.addBeanPropertySetter("incomingImage/imageName", "imageName");
        digester.addBeanPropertySetter("incomingImage/imageLocation", "imageLocation");
        digester.addBeanPropertySetter("incomingImage/scanOperator", "scanOperator");
        digester.addBeanPropertySetter("incomingImage/scanDate", "scanDateString");
        digester.addBeanPropertySetter("incomingImage/saveAttributes", "saveAttributes");
        
        digester.addObjectCreate("incomingImage/indexAttributes/attribute", Attribute.class);
        digester.addSetProperties("incomingImage/indexAttributes/attribute", "name", "name");
        digester.addSetProperties("incomingImage/indexAttributes/attribute", "value", "value");
        // Tells Digester to call addIndexAttribute() method when it finds the closing element matching the given pattern (first arg)
        digester.addSetNext("incomingImage/indexAttributes/attribute", "addIndexAttribute");
        
        digester.addObjectCreate("incomingImage/validationAttributes/attribute", Attribute.class);
        digester.addSetProperties("incomingImage/validationAttributes/attribute", "name", "name");
        digester.addSetProperties("incomingImage/validationAttributes/attribute", "value", "value");
        digester.addSetNext("incomingImage/validationAttributes/attribute", "addValidationAttribute");

        digester.addObjectCreate("incomingImage/searchAttributes/attribute", Attribute.class);
        digester.addSetProperties("incomingImage/searchAttributes/attribute", "name", "name");
        digester.addSetProperties("incomingImage/searchAttributes/attribute", "value", "value");
        digester.addSetNext("incomingImage/searchAttributes/attribute", "addSearchAttribute");
        
        // Now that rules are defined, start the parsing process
        IncomingImage incomingImage = (IncomingImage) digester.parse(new File("incomingImage-sample.xml"));
        
        System.out.println(incomingImage);

    }
    
    
    private static void parseExample() throws Exception
    {
    	Digester digester = new Digester();
    	digester.setValidating( false );

    	digester.addObjectCreate( "response", Response.class );

    	digester.addObjectCreate( "response/request", Request.class );
    	digester.addBeanPropertySetter("response/request/name", "name" );
    	digester.addBeanPropertySetter("response/request/value", "value");
    	digester.addSetNext( "response/request", "setRequest" );

    	digester.addBeanPropertySetter( "response/matches", "matches" );

    	Reader reader = new StringReader(
    			"<?xml version='1.0' encoding='UTF-8'?>" + 
    			"<response>" + 
    			"<request><name>books</name><value>xml</value></request>" +  
    			"<matches>20</matches>" + 
    			"</response>");
    	Response response = (Response)digester.parse( reader );
    	//Response response = (Response)digester.parse( new File("response.xml") );

    	System.out.println( response.toString() );

    }

    
    
}

